import {defineStore} from 'pinia'
import {useLogin} from '../api/user_api'
import type { LoginError, UserInfo } from '../model/user';
import type { workType } from '@/model/goods';
import http from '@/utils/http';
export const useCounterStore= defineStore('counter',{
    state:()=>{
        return{ 
            islogin:true as boolean,
            count:0,
            topheight:0,
            userData:null as UserInfo | null,
            collectedWorks: [] as workType[]
            }
    },
    actions:{
        add(){
            this.count++;
        },
        //文集数据获取
        async cllectedW (userid:string){
            console.log(this.userData?.objectId);
            let res= await http.get('/classes/collectedworks',{params:{
                where:{
                        "userid":userid
                }
            }})
            console.log('文集数据',res.data.results);
            this.collectedWorks=res.data.results as workType[]
            console.log('1567',this.collectedWorks[0]);
        },
        //登录
        async loginUser(username:string,password:string){
            let title='登录成功'
            let islogin=true
            try {
                let res=await useLogin(username,password)
                console.log('成功登录',res.data);
                this.islogin = true
                this.userData={
                    ...res.data,
                    islogin:islogin
                }
                console.log('111',this.userData);
                await uni.setStorage({
                    key:'userinfo',
                    data:this.userData
                })
                if(this.islogin){
                    uni.switchTab({
                        url:'/pages/home/home'
                    })
                }
            } catch (err) {
                title=(err as LoginError).data!.error
                this.islogin=false
            }
            uni.showToast({
                title,
                icon:'none'
            })
        }
        
    }
})